<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>
      <div class="login-form-form">
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
            <i slot="prefix" class="el-input__icon el-icon-user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
            @keyup.enter.native="handleLogin"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex; justify-content: space-between">
            <el-checkbox v-model="checkPwd">记住密码</el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            round
            :loading="loading"
            type="primary"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from "@/utils/validate";
import Cookies from "js-cookie";
import CryptoJs from "crypto-js";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("账号不能为空"));
      } else if (value.length < 5) {
        callback(new Error("请输入至少由数字、英文、汉语组成的5位账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("密码不能为空"));
      } else if (value.length < 5) {
        callback(new Error("请输入至少由数字、英文组成的5位密码"));
      } else {
        callback();
      }
    };
    return {
      checkPwd: "",
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              // 判断是否有选中记住密码
              if (this.checkPwd) {
                // 保存账号以及密码加密,此处userPwd为秘钥
                Cookies.set("username", this.loginForm.username.trim(), {
                  expires: 30,
                });
                Cookies.set(
                  "password",
                  CryptoJs.AES.encrypt(this.loginForm.password, "userPwd"),
                  { expires: 30 }
                );
              } else {
                Cookies.remove("username");
                Cookies.remove("password");
              }
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
  created(){
    // 判断Cookies是否有数据
    const username = Cookies.get('username')
    const password = Cookies.get('password')
    if(username && password){
      // 密码解密以及数据回填
      this.loginForm.username = username
      this.loginForm.password = CryptoJs.AES.decrypt(password,'userPwd').toString(CryptoJs.enc.Utf8)
      this.checkPwd = true
    }else{
      this.checkPwd = false
    }
  }
};
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$cursor: #fff;
$light_gray: #203892;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 100%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 48px;
      caret-color: $cursor;

      &:-webkit-autofill {
        -webkit-text-fill-color: $cursor !important;
      }
    }
    ::v-deep .el-input__inner {
      border-radius: 24px;
      color: $light_gray;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #203892;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("~@/assets/login-bg.jpg") no-repeat;
  overflow: hidden;
  display: flex;
  justify-content: end;
  padding: 50px;
  .login-form {
    background: white;
    width: 520px;
    height: 50%;
    max-width: 100%;
    align-self: center;
    padding: 135px 60px;
    .el-button {
      width: 100%;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
